﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Collections;
using yq.Dictionary;

namespace yq.ControllerBase
{
    /// <summary>
    /// 当前用户
    /// </summary>
    [Serializable]
    public class CurrentUser
    {
        private const string Key = "CurrentUser";

        /// <summary>
        /// 是否在线
        /// </summary>
        public bool IsLogin
        {
            get
            {
                yq.Model.Logic.UserAccount user = GetUser();

                return user != null;
            }
        }

        /// <summary>
        /// 用户ID
        /// </summary>
        public string UserId
        {
            get
            {
                yq.Model.Logic.UserAccount user = GetUser();

                return user == null ? "" : user.UserId;

            }
        }

        /// <summary>
        /// 用户权限
        /// </summary>
        public  yq.Enums.UserAccount.PurviewEnum PurviewEnum
        {
            get
            {
                yq.Model.Logic.UserAccount user = GetUser();

                return user == null ? yq.Enums.UserAccount.PurviewEnum.Visitor : user.PurviewEnum;
            }
        }


        /// <summary>
        /// 获取当前用户信息
        /// </summary>
        /// <returns></returns>
        public  yq.Model.Logic.UserAccount GetUser()
        {
            return HttpContext.Current.Session==null? new yq.Model.Logic.UserAccount():  HttpContext.Current.Session[Key] as yq.Model.Logic.UserAccount;
        }

         /// <summary>
        /// 特殊权限查询
        /// </summary>
        /// <returns>是否可以访问本页面</returns>
        public Enums.PurviewException.Type GetPurview(string key)
        {
            if (IsLogin)
            {
                //组成特殊权限ID
                key = UserId + "_" + key;
                
                //查询是否有特殊权限
                var purviewModel = yq.Model.Logic.PurviewException.Load(key, Basis.CaCheLocdType.CaChe);

                return purviewModel.Type;
            }
            else
            {
                return Enums.PurviewException.Type.Default;
            }
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public ReturnValue<string> Login(string userId)
        {
            ReturnValue<string> rv = new ReturnValue<string>();
            var info = yq.Model.Logic.UserAccount.Load(userId);

            if (info.IsLoad)
            {
                if (info.State == Enums.UserAccount.State.Normal)
                {
                    rv.True(info.UserId);
                    SetUser(info);
                }
                else if (info.State == Enums.UserAccount.State.Stop)
                {
                    rv.False("账号已停用");
                }
                else if (info.State == Enums.UserAccount.State.NoActivated)
                {
                    rv.False("账号未激活");
                }
                else
                {
                    rv.False("账号状态不正确");
                }
            }
            else
            {
                rv.False("未找到关联账号");
            }

            return rv;
        }



        /// <summary>
        /// 写入用户
        /// </summary>
        /// <param name="user"></param>
        public  void SetUser(yq.Model.Logic.UserAccount user)
        {
            HttpContext.Current.Session[Key] = user;
        }
    }
}
